<?php
declare (strict_types = 1);

// +----------------------------------------------------------------------
// | i18n语言包工具
// +----------------------------------------------------------------------
// | 版权所有 2019~2022 saruri 系统集成中国有限公司 [ http://www.saruri.cn ]
// +----------------------------------------------------------------------
// | 官方网站: https://saruri.cn
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github 代码仓库：https://gitee.com/saruri/languagePackage.git
// +----------------------------------------------------------------------

//附加列表数据
namespace service;

use PhpOffice\PhpSpreadsheet\IOFactory;
use \Overtrue\Pinyin\Pinyin;

/**
 * 代码生成的AnnoucementService服务
 * Class AnnoucementService
 * @package app\admin\service
 */
class SaruriService
{

    /**
     * 启动服务
     * @return void
     */
    public function run($arr)
    {
        //exit("执行了SaruriService服务");
        //获取excel文件
        var_dump($arr);
        echo '您的输入excel:' . $arr['e'] . ',js 名称' . $arr['j'] . ',准备开始进行转换,请稍等...';
        $arr['excel'] = $arr['e'] . ".xlsx";
        $this->languauage = $arr['j']; //根据js 选择  如果是zh 则是数组的[0] 其他为[1]
        $data = $this->getData($arr);

        //$data=array_unique($data);
        $data = $this->pinyin($data);
        $data = $this->checkData($data);
        //var_dump($data);
        $str = $this->build($data);
        $this->saveData($str);
        //exit($str);
        //$this->runCommand();

    }

    /*
     *  @desc      //构造js 文件
     *  @author    saruri <saruri@163.com>
     *  @date      2022/07/15 17:04:22
     */
    public function build(array $data)
    {
        $string = "export default {" . "\r\n";
        $end = array_key_last($data);
        //exit($end);
        foreach ($data as $key => $value) {

            if ($key == $end) {
                $string .= $key . ":" . "'" . $value . "'" . "\r\n";
            }
            if ($key != $end) {
                $string .= $key . ":" . "'" . $value . "'," . "\r\n";
            }
        }

        $string .= "}";

        return $string;
    }

    //处理数组 去重并语言选择
    public function checkData(array $data)
    {
        $arr = [];
        foreach ($data as $key => $value) {
            if ($this->languauage == 'zh') {
                $arr[$value['pinyin']] = $value[0];
            } else {
                $arr[$value['pinyin']] = $value[1];
            }

        }
        return $arr;
    }

    //保存数据到文件
    public function saveData(string $str)
    {

        $fileName = $this->languauage . ".js";
        $file = fopen($fileName, "w");
        fwrite($file, $str);
        fclose($file);
        //$handle = fopen($file, "w");
        //file_put_contents($file, $str);
    }

    //转换拼音
    public function pinyin($data)
    {

        //vendor('overtrue.pinyin.src.Pinyin');
        $pinyin = new \Overtrue\Pinyin\Pinyin();

        //$pinyin = new Pinyin();
        //$pinyin = new /Overtrue/Pinyin/Pinyin();
        //$pinyin = new Pinyin(); // 默认
        foreach ($data as $key => $value) {
            $data[$key]['pinyin'] = $pinyin->permalink($value[0], '.');
            $data[$key]['pinyin'] = \str_replace(".", "", $data[$key]['pinyin']);
        }
        return $data;
    }

    /**
     * 获取数据
     * @return void
     */
    public function getData($path)
    {
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
        $spreadsheet = $reader->load($path['excel']);
        $sheet = $spreadsheet->getActiveSheet();
        //\halt($sheet);

        $sheetData = $sheet->toArray(null, true, true, false);
        return $sheetData;

    }

    /**
     * 初始化配置信息
     * @return void
     */
    public function initConfig()
    {
        // 初始化配置信息
        $this->initConfig();
    }

    /**
     * 初始化路径信息
     * @return void
     */
    public function initPath()
    {
        // 初始化路径信息
        $this->initPath();
    }

}
